package com.example.basic.png;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException {

        File file = new File("./bainiangudu.0");
        System.out.println(file.getAbsolutePath());
        int indexOf = file.getAbsolutePath().lastIndexOf(File.separator);
        System.out.println(file.getAbsolutePath().substring(0,indexOf));

        String[] split = file.getName().split("\\.");
        System.out.println(split.length);

        int indexOf1 = file.getName().lastIndexOf(".");
        System.out.println(indexOf1);
        String nnn = file.getName().substring(0,indexOf1);
        System.out.println(nnn);

//        byte[] buf = new byte[2];

//        if (file.exists()) {
//            System.out.println("文件存在");
//
//            try {
//                InputStream inputStream = new FileInputStream(file);
//
//                int count;
//                do  {
//                    count = inputStream.read(buf);
//
//                    System.out.println("成功读到了" + count + "个字节   值为：" + buf[0] + ";" + buf[1]);
//                }while (count != -1);
//
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//
//            FileOutputStream fileOutputStream = new FileOutputStream(file);
//            fileOutputStream.
//
//
//
//        } else {
//            System.out.println("文件不存在");
//        }


//        byte[] header = new byte[]{(byte) 0x89,0x50,0x4e,0x47};
//
//        try {
//            RandomAccessFile src = new RandomAccessFile(file, "rw");
//            long length = src.length();
//            // 略过前4个字节
//
//            src.skipBytes(4);
//
//            byte[] buff = new byte[(int) (length - 4)];
//
//// 读取除前两个字节之后的字节
//
//            src.read(buff);
//
//            src.seek(0);
//
//            src.write(header);
//
//            src.seek(header.length);
//
//            src.write(buff);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


//        getImagePixel();
//        transformGray_R("./1.jpg","./2.jpg");


    }


    /**
     * 读取一张图片的RGB值
     */
    public static void getImagePixel() {

        int[] rgb = new int[3];
        File file = new File("./1.jpg");
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (IOException e) {

            e.printStackTrace();
        }
        int width = bi.getWidth();
        int height = bi.getHeight();
        System.out.println(width + ";" + height);
        int minX = bi.getMinX();
        int minY = bi.getMinY();
        for(int y = minY; y < height; y++) {
            for(int x = minX; x < width; x++) {
                //获取包含这个像素的颜色信息的值, int型
                int pixel = bi.getRGB(x, y);
                //从pixel中获取rgb的值
                rgb[0] = (pixel & 0xff0000) >> 16; //r
                rgb[1] = (pixel & 0xff00) >> 8; //g
                rgb[2] = (pixel & 0xff); //b
//                System.out.print("("+Integer.toHexString(rgb[0]) + "," + Integer.toHexString(rgb[1]) + "," + Integer.toHexString(rgb[2]) + ")");
            }
            System.out.println();
        }

    }

    /**
     * @param imagePath 要灰化图像的名字
     * @param path 生成的图像的名字
     *
     */
    public static void transformGray_R(String imagePath, String path) {
        BufferedImage image;
        try {
            image = ImageIO.read(new File(imagePath));
            for(int y = image.getMinY(); y  < image.getHeight(); y++) {
                for(int x = image.getMinX(); x < image.getWidth(); x ++) {
                    int pixel = image.getRGB(x, y);
                    int r = (pixel >> 16) & 0x00ff;
                    pixel = (r & 0x000000ff) | (pixel & 0xffffff00); //用r的值设置b的值
                    pixel = ((r<<8) & 0x0000ff00) | (pixel & 0xffff00ff);//用r的值设置g的值
                    image.setRGB(x, y, pixel);
                }
            }
            try {
                ImageIO.write(image, "jpg", new File(path));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }


}
